/**
 * 
 */
package com.yidu.drug.action;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yidu.drug.action.vo.FdPoBreakdownModelVo;
import com.yidu.drug.common.Message;
import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.model.DrugModel;
import com.yidu.drug.model.FdPoBreakdownModel;
import com.yidu.drug.model.FdPurchaseModel;
import com.yidu.drug.model.WholesaleModel;
import com.yidu.drug.service.FdPoBreakdownModelService;
import com.yidu.drug.service.FdPurchaseService;

/**
 * 订单表
 * @author yuanwei
 *
 */
@Controller
@RequestMapping("/FdPurchase")
public class FdPurchaseAction {
	
	@Resource
	private FdPurchaseService fdPurchaseService;
	
	@Resource
	private FdPoBreakdownModelService fdPoBreakdownModelService;
	
	@RequestMapping("/findRow")
	@ResponseBody
	public Page findRow(HttpSession session,FdPurchaseModel fdPurchaseModel){
		int total=fdPurchaseService.findRows(fdPurchaseModel);
		System.out.println("总行数"+total+"---------------------");
		Page page = new Page();
		int pages = 0;
		if(total % 10 == 0){
			pages = total / 10;
		}else {
			pages = total / 10 + 1;
		}
		page.setPage(pages);
		page.setTotalRows(total);
		page.setRows(10);
		return page;
		
	}
	
	/**
	 * 查询订单表中的所有数据
	 * @return
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public Map<String, Object> findAll(String fdPoPerson,int page,int limit){
		FdPurchaseModel fdPurchaseModel=new FdPurchaseModel();
		fdPurchaseModel.setFdPoPerson(fdPoPerson);
		page=(page-1)*limit;
		Page pages = new Page();
		pages.setPage(page);
		pages.setRows(limit);
		List<FdPurchaseModel> list=fdPurchaseService.findAll(fdPurchaseModel,page,limit);
		Map<String, Object> map=new HashMap<String , Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",fdPurchaseService.findRows(fdPurchaseModel));
		map.put("data", list);
		return map;
	}
	
	/**
	 * 删除
	 * @param merchantType 传入一个商家类型对象
	 * @return 返回一个字符串
	 */
	@RequestMapping("/deletes")
	@ResponseBody
	public String delete(String fdPoId){
		System.out.println(fdPoId+"========================");
		int rows =fdPurchaseService.delete(fdPoId);
		fdPoBreakdownModelService.deletes(fdPoId);
		if(rows > 0) {
			return "ok";
		}else {
			return "no";
		}
	}
	/**
	 * 根据订单id查询数据
	 * @param fdPoId
	 * @return
	 */
	@RequestMapping("/findById")
	@ResponseBody
	public FdPurchaseModel findById(String fdPoId) {
		 
		return fdPurchaseService.findById(fdPoId);
		
	}
	
	/**
	 * 根据订单id修改数据
	 * @param fdPurchaseModel
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public String update(FdPurchaseModel fdPurchaseModel) {
		System.out.println(fdPurchaseModel.getFdPoId()+"进来了=============================================");
		int rows =fdPurchaseService.update(fdPurchaseModel);
		Integer sl=fdPoBreakdownModelService.findAllsl(fdPurchaseModel.getFdPoId());
		Integer jg=fdPoBreakdownModelService.findAlljg(fdPurchaseModel.getFdPoId());
		
		System.out.println(sl+"数量========================================");
		System.out.println(jg+"价格=========================");
		fdPurchaseService.updates(sl.longValue(), new BigDecimal(jg), fdPurchaseModel.getFdPoId());
		if(rows > 0) {
			return "ok";
		}else {
			return "no";
		}
		
	}
	/**
	 * 新增订单
	 * @param session
	 * @param fdPoRemark
	 * @param fdPoBreakdownModel
	 * @param arr
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(HttpSession session,String fdPoRemark,FdPoBreakdownModel fdPoBreakdownModel,String arr) {
		//new 一个对象订单表对象
		FdPurchaseModel fdPurchaseModel=new  FdPurchaseModel();
		//随机一个id
		String fdPoId="CG"+Tools.getDateOrderNo();
		Integer sj=Tools.getRandomNumber(10000);
		//得到当前时间
		Timestamp t = new Timestamp(new Date().getTime());
		//将审核状态默认为1.等待审核
		String a="1";
		//加入id
		fdPurchaseModel.setFdPoId(fdPoId);
		//加入登陆人id
		fdPurchaseModel.setManId("1");
		//加入登陆人名字
		fdPurchaseModel.setFdPoPerson("唐磊");
		//加入生成订单的时间
		fdPurchaseModel.setFdPoTime(t);
		//加入审核状态
		fdPurchaseModel.setFdPoState(a);
		//加入订单备注
		fdPurchaseModel.setFdPoRemark(fdPoRemark);
		//加入批次号
		fdPurchaseModel.setFdNumber("批:"+t+sj);
		//加入到订单表中
		int rows=fdPurchaseService.save(fdPurchaseModel);
		System.out.println(arr+"========================================================");
		//增加订单详情表的数据
		Gson gosn=new Gson();
		//创建一个提示消息的对象
		Message msg = new Message();
		//使用Type类，取得相应类型对象的class属性
		java.lang.reflect.Type listType=new TypeToken<ArrayList<FdPoBreakdownModelVo>>(){}.getType();//TypeToken内的泛型就是Json数据中的类型
		//使用集合接受将json转换为java对象集合后的数据
		ArrayList<FdPoBreakdownModelVo> list = gosn.fromJson(arr, listType);
		for (int i=1;i<list.size();i++) {
			System.out.println(list.get(i).getDrugId()+"==========================================");
			System.out.println(list.get(i).getFdItemNumber()+"------------------------------------");
			System.out.println(list.get(i).getFdItemRemark()+"===================================");
			System.out.println(list.get(i).getFdUnit()+"---------------------------------------");
			System.out.println(list.get(i).getFdPrice()+"======================================");
			System.out.println(list.get(i).getFdItemNumber()*Integer.valueOf(list.get(i).getFdPrice())+"====================");
			//设置一个随机id
			String fdPodId=UUID.randomUUID().toString().replaceAll("-", "");
			//加入id
			fdPoBreakdownModel.setFdPodId(fdPodId);
			//加入订单详情表id
			fdPoBreakdownModel.setFdPoId(fdPoId);
			//加入药品表id
			fdPoBreakdownModel.setDrugId(list.get(i).getDrugId());
			//加入药品数量
			fdPoBreakdownModel.setFdItemNumber(list.get(i).getFdItemNumber().longValue());
			//根据药品数量和价格计算出该药品的总金额
			fdPoBreakdownModel.setFdSubtotal(new BigDecimal(list.get(i).getFdItemNumber()*Integer.valueOf(list.get(i).getFdPrice())));
			//加入药品备注
			fdPoBreakdownModel.setFdItemRemark(list.get(i).getFdItemRemark());
			//加入药品单位
			fdPoBreakdownModel.setFdUnit(list.get(i).getFdUnit());
			//加入进货单价
			fdPoBreakdownModel.setFdPrice(new BigDecimal(list.get(i).getFdPrice()));
			//加入到数据库
			fdPoBreakdownModelService.save(fdPoBreakdownModel);
		}
		Integer sl=fdPoBreakdownModelService.findAllsl(fdPoId);
		Integer jg=fdPoBreakdownModelService.findAlljg(fdPoId);
		
		System.out.println(sl+"数量========================================");
		System.out.println(jg+"价格=========================");
		fdPurchaseService.updates(sl.longValue(), new BigDecimal(jg), fdPoId);
		if(rows > 0) {
			return "ok";
		}else {
			return "no";
		}
		//redirect:/pages/yuanwei/purchase.html
	}
	/**
	 * 审核查询所有和条件查询分页
	 * @param response 响应
	 * @param page 页数
	 * @param limit 行数
	 * @return 批发订单
	 */
	@RequestMapping("findAllCwsh")
	@ResponseBody
	public Map<String, Object> findAllCwsh(HttpServletResponse response,FdPurchaseModel fdPurchase,int page,int limit,String dateOne,String dateTwo) {
		
		response.setCharacterEncoding("UTF-8");
		Page pages = new Page();
		pages.setPage(page);
		pages.setRows(limit);
		Map<String,String> map=new HashMap<>();
		map.put("fdPoPerson", fdPurchase.getFdPoPerson());
	/*	map.put("filId", "1");
		map.put("isva", "1");*/
		map.put("start", String.valueOf(pages.getStart()));
		map.put("rows", String.valueOf(pages.getRows()));
		if(!Tools.isEmpty(dateOne)&&!Tools.isEmpty(dateTwo)){
			if(dateOne.compareTo(dateTwo)>0){
				map.put("dateOne", dateTwo);
				map.put("dateTwo", dateOne);
			}else{
				map.put("dateOne", dateOne);
				map.put("dateTwo", dateTwo);
			}
		}else if(!Tools.isEmpty(dateOne)&&Tools.isEmpty(dateTwo)){
			map.put("dateOne", dateOne);
		}else if(!Tools.isEmpty(dateTwo)&&Tools.isEmpty(dateOne)){
			map.put("dateTwo", dateTwo);
		}
		
		List<FdPurchaseModel> list = fdPurchaseService.findAllPage(map);
		Map<String, Object> maps = new HashMap<>();
		//需要符合layUI格式，code msg count data
		maps.put("code", 0);
		maps.put("msg", "1");
		maps.put("count", fdPurchaseService.findRows(fdPurchase));
		maps.put("data", list);
		fdPurchase=new FdPurchaseModel();
		
		return maps;
	}
}
